const fs=require('fs');

function findF(path){
    path =path || './controllers';
    let file=fs.readdirSync(path);
    return file.filter(files=>{
        return files.endsWith('.js') && files !== 'index.js';
    })
}

function regiestryR(files,router){
    files.forEach(file=>{
        let tmpmodule=require('../controllers/'+file);
        for(let key in tmpmodule){
            let tmpArr=key.split(' ');

            let method=tmpArr[0];
            let url=tmpArr[1];
            let fn=tmpmodule[key];

            if(method=== 'get'){
                router.get(url,fn);
            }else if(method=== 'post'){
                router.post(url,fn);
            }else if(method === 'put'){
                router.put(url,fn);
            }else if(method === 'del'){
                router.delete(url,fn)
            }
        }
    })
}


module.exports={
    findF,
    regiestryR
}